package com.park.manage.service.meruser.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.park.comm.ResultEnum;
import com.park.dao.mapper.meruser.MerUserInfoMapper;
import com.park.dao.mapper.order.ParkOrderInfoMapper;
import com.park.entity.mer.MerUserInfo;
import com.park.entity.mer.MerUserItem;
import com.park.entity.page.PageInfoParm;
import com.park.exception.CommException;
import com.park.manage.service.meruser.MerUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Administrator
 * @title: MerUserServiceImpl
 * @projectName park
 * @description: 管理台-商户用户
 * @date 2020/5/510:23
 */
@Service(value = "merUserService")
public class MerUserServiceImpl implements MerUserService {

    @Autowired
    private MerUserInfoMapper merUserInfoMapper;

    @Autowired
    private ParkOrderInfoMapper parkOrderInfoMapper;

    /**
 　　* @description:  商户添加
 　　* @param [merUserInfo]
 　　* @return
 　　* @throws
 　　* @author ZhangHaiWen
 　　* @version 2.0
 　　* @date 2020/5/5 10:25
 　　*/
    public void add(MerUserInfo merUserInfo) {
        if(ObjectUtil.isEmpty(merUserInfo)){
            throw new CommException(ResultEnum.MER_USER_NAME_EMPTY_ERROR);
        }
        String merPhoneNumber = merUserInfo.getMerPhoneNumber();
        MerUserInfo byMobilePhone = merUserInfoMapper.getByMobilePhone(merPhoneNumber);
        if(ObjectUtil.isNotEmpty(byMobilePhone)){
            throw new CommException(ResultEnum.MER_USER_USER_EXIST_ERROR);
        }
        String merPassword = merUserInfo.getMerPassword();
        if(StrUtil.isBlank(merPassword)){
            throw new CommException(ResultEnum.MER_USER_PASSWORD_EMPTY_ERROR);
        }
        merUserInfo.setMerPassword(SecureUtil.md5(merPassword));
        long timeMillis = System.currentTimeMillis();
        merUserInfo.setAddTimeStamp(timeMillis);
        merUserInfo.setTimeStamp(timeMillis);
        merUserInfoMapper.insert(merUserInfo);
    }

    /**
     　　* @description:  查询商户列表 订单总数和订单金额 只统计 出停车场的
     　　* @param [merUserInfo]
     　　* @return
     　　* @throws
     　　* @author ZhangHaiWen
     　　* @version 2.0
     　　* @date 2020/5/5 10:25
     　　*/
    public PageInfo<MerUserItem> list(PageInfoParm pageInfoParm) {
        int pageSize = pageInfoParm.getPageSize();
        int pageNum = pageInfoParm.getPageNum();
        PageHelper.startPage(pageNum,pageSize);
        List<MerUserItem> merList = merUserInfoMapper.getParkOrderList();
        PageInfo<MerUserItem> pageInfo = new PageInfo<MerUserItem>(merList);
        return pageInfo;
    }
}
